angular.module('planAdd', ['toastr', 'ui.select', 'common']).
    directive('bootstrapSwitch', function () {
        return {
            restrict: 'A',
            scope: {
                sync: '=bootstrapSwitch'
            },
            link: function (scope, element, attrs) {
                if (scope.sync) {
                    $(element).attr("checked", "checked");
                }
                $(element).bootstrapSwitch();
                $(element).on('switchChange.bootstrapSwitch', function (e, data) {
                    scope.sync = data;
                    $.post('/setting/update', {status: (data ? "on" : "off")}, function (data) {
                        if (data === 'success') {
                            if (scope.sync) {
                                $(".toggle-left").each(function () {
                                    $(this).attr('disabled', 'disabled');
                                });
                                $(".toggle-right").each(function () {
                                    $(this).removeAttr("disabled");
                                    $(".ui-select-search").removeAttr('disabled');
                                });
                            } else {
                                $(".toggle-right").each(function () {
                                    $(this).attr('disabled', 'disabled');
                                    $(".ui-select-search").attr('disabled', 'disabled');
                                });
                                $(".toggle-left").each(function () {
                                    $(this).removeAttr('disabled');
                                });
                            }
                            scope.$apply();
                        } else {
                            alert(data.description);
                            scope.sync = !scope.sync;
                            scope.$apply();
                        }
                    });
                });
            }
        }
    }).controller('planAddController',
    ['$scope', '$http', 'toastr', 'commonService',
        function ($scope, $http, toastr,commonService) {
            $scope.autoPublish = !($("#switchValue").attr("value") === 'off');
            if ($scope.autoPublish) {
                $(".toggle-left").each(function () {
                    $(this).attr('disabled', 'disabled');
                });
            } else {
                $(".toggle-right").each(function () {
                    $(this).attr('disabled', 'disabled');
                    setTimeout(function(){$(".ui-select-search").attr('disabled', 'disabled')},100);
                });
            }

            $("#start").datetimepicker({
                format : 'Y/m/d',
                timepicker:false,
                onClose : function(){
                    $scope.plan.start_date = $("#start").val();
                }
            });
            $("#end").datetimepicker({
                format : 'Y/m/d',
                timepicker:false,
                onClose : function(){
                    $scope.plan.end_date = $("#end").val();
                }
            });

            $scope.weekMap = [
                {id: 0, value: "星期一"},
                {id: 1, value: "星期二"},
                {id: 2, value: "星期三"},
                {id: 3, value: "星期四"},
                {id: 4, value: "星期五"},
                {id: 5, value: "星期六"},
                {id: 6, value: "星期日"}
            ];

            $scope.plan = {
                plan_name: '',
                start_date: new Date().Format('yyyy/MM/dd'),
                end_date: new Date(new Date().valueOf() + 86400 * 14 * 1000).Format('yyyy/MM/dd'),
                next_plan: ''
            };

            $scope.setting = {
                name_tpl: $("#nameTpl").val(),
                next_tpl: $("#nextTpl").val(),
                freq_interval: parseInt($("#freqInterval").val()),
                freq_week: $("#freqWeek").attr("value")
            };

            $scope.emitPlan = function () {
                if(!$scope.plan.plan_name) {
                    toastr.warning("输入不完整");
                    return;
                }
                if($scope.plan.start_date > $scope.plan.end_date) {
                    toastr.warning("开始时间不得晚于结束时间");
                    return;
                }

                commonService.modalPrompt("发布新计划后当前计划无法更改,确认发布?",function(){
                    if(!$scope.plan.next_plan) {
                        $scope.plan.next_plan = "暂无";
                    }
                    $http.post('/plan/add', $scope.plan).success(function (data) {
                        if (data === 'success') {
                            toastr.success('发布计划安排成功,已通知各研发组长');
                        } else {
                            toastr.error('发布失败,' + data.description);
                        }
                    }).error(function () {
                        toastr.error("请检查网络连接");
                    });
                });
            };

            $scope.saveSetting = function () {
                $scope.setting.freq = $scope.setting.freq_interval + "/" + $scope.setting.freq_week;
                if (!$scope.setting.next_tpl) {
                    $scope.setting.next_tpl = "暂无";
                }
                if (!$scope.setting.name_tpl || $scope.setting.freq_interval < 1) {
                    toastr.warning('输入有误');
                    return;
                }
                $http.post('/setting/update', $scope.setting).success(function (data) {
                    if (data === 'success') {
                        toastr.success('保存成功');
                    } else {
                        toastr.error('保存失败,' + data.description);
                    }
                }).error(function () {
                    toastr.error("请检查网络连接");
                });
            }
        }
    ]);